{extend name="public:base"}
{block name="body"}
<body class="pear-container">
	<div class="layui-card">
		<div class="layui-card-body">
			<table id="table-list" lay-filter="table-list"></table>
		</div>
	</div>
</body>
{/block}

{block name="script"}
<script type="text/html" id="table-image">
    <div class="thumb" style="width:30px;height:30px;display:inline-block;"><img src="{{ d.image }}" width="30" height="30"></div>
</script>
<script type="text/html" id="table-position">
    <span>{$position}</span>
</script>
<script type="text/html" id="table-create_time">
        {{layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm')}}
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let cols = [
            [{
                type: 'checkbox'
            },
            {
                title: '标题',
                field: 'title',
                align: 'left',
                width:200
           
            },
            {
                title: '图片',
                field: 'image',
                align: 'center',
                templet:'#table-image',
                width:100
            },
            {
                title: 'URL',
                field: 'url',
                align: 'center',
                width:150
            },
            {
                title: '广告位',
                field: 'position_id',
                align: 'center',
                templet: '#table-position',
  
            },
            {
                title: '排序',
                field: 'sort',
                align: 'center',
                width: 150
            },
            {
                title: '创建时间',
                field: 'create_time',
                align: 'center',
                templet: '#table-create_time',
                width: 150
            },
            {
                title: '操作',
                toolbar: '#table-bar',
                align: 'center',
                width: 130
            }
            ]
        ]

        table.render({
            elem: '#table-list',
            url: '{:url("index")}?position_id={:input("position_id")}',
            parseData: function(res){ 
            return {
            "code": res.code, //解析接口状态
            "msg": res.message, //解析提示文本
            "count": res.data.total, //解析数据长度
            "data": res.data.data //解析数据列表
            }},
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#table-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });
        form.on('submit(table-query)', function (data) {
            table.reload('table-list', {
                where: data.field
            })
            return false;
        });
        table.on('tool(table-list)', function (obj) {
            if (obj.event === 'remove') {
                common.remove(obj,"{:url('batch')}");
            }
            if (obj.event === 'edit') {
                common.modalo('{:url("edit")}?position_id={:input("position_id")}&ids=' + obj.data['id'],'编辑','540px');
            }
        });

        table.on('toolbar(table-list)', function (obj) {
            if (obj.event === 'add') {
               common.modalo('{:url("add")}?position_id={:input("position_id")}','新增','540px');
            } else if (obj.event === 'refresh') {
                common.refresh();
            } else if (obj.event === 'batchRemove') {
                common.batchRemove(obj,"{:url('batch')}");
            }
        });
        $(document).on('click','.thumb',function(){
            var src = $(this).find('img').attr('src');
 
            layer.open({
                type: 1,
                title: false,
                shade: 0.8,
                closeBtn: 0,
                shadeClose: true,
                content: '<img src="'+src+'">'
            });
        })
        form.on('switch(table-status)', function (data) {
            let loading = layer.load();
            let field = {};
            field['id']=data.value;
            field[data.elem.name] = data.elem.checked?"1":"0";
            $.ajax({
                url: '{:url("change")}',
                type: 'post',
                data: field,
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg('操作成功', {
								icon: 1,
								time: 1000
							})
                    } else {
                        layer.msg(result.msg, {
								icon: 2,
								time: 1000
							})
                    }
                }
            })
        });  
    })
</script>
{/block}